home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Light ROM 1
/
LIGHT-ROM 1 (Amiga Library Services)(1994).iso
/
ffdisks
/
d939.lha
/
ExtraCmds
/
source_etc.lha
/
src
/
Head.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-22
|
5KB
|
210 lines
/* --------------------------------- -------
* |\ | | | | | |.| | \| |/ /|\ |||||||
* | | | |/ | |\ |/ |/| |\ |/ | ? ---+--- =<
* | | | | | | | | | | | \qqqqqqqqq/
* --------------------------------- ~~~~~~~~~~~~~~~~
* Head - Display first few lines of specified files.
* Copyright (C) 1992, 1993 Torsten Poulin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* The author can be contacted by s-mail at
* Torsten Poulin
* Banebrinken 99, 2, 77
* DK-2400 Copenhagen NV
* DENMARK
*
* Created 11-Feb-92 Version 37.1 =TP=
* $Id: Head.c,v 37.7 93/03/01 12:36:57 Torsten Rel $
* $Log: Head.c,v $
* Revision 37.7 93/03/01 12:36:57 Torsten
* Changed all occurrences of "struct DosBase *" to "struct DosLibrary *"
*
* Revision 37.6 93/02/24 00:13:13 Torsten
* Bug fix: called FreeArgs() too soon.
* The TO argument is now functional.
* Eliminated goto statement in entrypoint().
* Removed unnecessary auto variable err in head().
*
* Revision 37.5 93/02/22 17:37:22 Torsten
* Moved some functions to the support library.
*
* Revision 37.4 93/02/20 11:51:12 Torsten
* head() now returns the correct value, if a Ctrl-C is recieved.
*
* Revision 37.3 93/02/20 11:45:08 Torsten
* Rewritten almost from scratch.
*
* Revision 37.2 93/02/13 15:56:39 Torsten
* Reformatted source.
* Replaced exec.library/SetSignal() with dos.library/CheckSignal().
*
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#ifdef __SASC
#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#endif
#include "tastlib.h"
#include "head_rev.h"
#define PROGNAME "Head"
#define TEMPLATE "FROM/M,TO/K,NUM=NUMBER/N/K"
#define OPT_FROM 0
#define OPT_TO 1
#define OPT_NUM 2
char const versionID[] = VERSTAG;
char const copyright[] = "$COPYRIGHT:Copyright © 1992,1993 Torsten Poulin$";
typedef struct {
struct DosLibrary *DOSBase;
ULONG number;
BOOL header;
BPTR output;
} Global;
LONG head(UBYTE *filename, Global *global);
LONG entrypoint(VOID)
{
struct DosLibrary *DOSBase;
struct RDArgs *args;
Global *global;
LONG arg[3];
LONG rc = RETURN_OK;
if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
return RETURN_FAIL;
if (!(global = AllocVec(sizeof(Global), MEMF_PUBLIC | MEMF_CLEAR)))
{
PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
rc = RETURN_FAIL;
}
else
{
global->DOSBase = DOSBase;
arg[OPT_FROM] = arg[OPT_TO] = arg[OPT_NUM] = 0L;
if (!(args = ReadArgs(TEMPLATE, arg, NULL)))
{
printerror(PROGNAME, global);
rc = RETURN_ERROR;
}
else
{
if (!arg[OPT_NUM])
global->number = 10L;
else
{
global->number = *(ULONG *) arg[OPT_NUM];
if (global->number < 1L)
global->number = 1L;
}
if (!arg[OPT_TO])
global->output = Output();
else if (!(global->output = Open((UBYTE *)arg[OPT_TO], MODE_NEWFILE)))
{
PutStr("Cannot open ");
PutStr((UBYTE *) arg[OPT_TO]);
PutStr("\n");
rc = RETURN_ERROR;
}
if (global->output)
if (!arg[OPT_FROM])
rc = head(NULL, global);
else if ((rc = severalnames((UBYTE **) arg[OPT_FROM], global))
!= ERROR_NO_FREE_STORE)
{
global->header = (BOOL) rc;
rc = foreach((UBYTE **) arg[OPT_FROM], head, global);
}
if (arg[OPT_TO])
Close(global->output);
FreeArgs(args);
if (rc == ERROR_BREAK)
{
PrintFault(ERROR_BREAK, NULL);
rc = RETURN_WARN;
}
else if (rc == ERROR_NO_FREE_STORE)
{
PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
rc = RETURN_FAIL;
}
else if (rc != RETURN_OK)
printerror(PROGNAME, global);
}
FreeVec(global);
}
CloseLibrary((struct Library *) DOSBase);
return rc;
}
LONG head(UBYTE *filename, Global *global)
{
struct DosLibrary *DOSBase = global->DOSBase;
register UBYTE breakcheck = 0;
LONG c;
ULONG count = 0;
LONG rc = RETURN_OK;
BPTR input = Input();
if (filename)
{
if (global->header)
{
FPuts(global->output, "==>");
FPuts(global->output, filename);
FPuts(global->output, "<==\n");
}
if (!(input = Open(filename, MODE_OLDFILE)))
{
PrintFault(IoErr(), filename);
return RETURN_ERROR;
}
}
while ((c = FGetC(input)) != EOF && count < global->number)
{
if (c == '\n')
++count;
FPutC(global->output, c);
if (!(breakcheck -= 4) && CheckSignal(SIGBREAKF_CTRL_C))
{
rc = ERROR_BREAK;
break;
}
}
if (filename)
Close(input);
return rc;
}